package com.example.ad.auth;

import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.command.ConnectionInfo;
import org.apache.activemq.security.AbstractAuthenticationBroker;
import org.apache.activemq.security.SecurityContext;

import java.nio.file.attribute.GroupPrincipal;
import java.security.Principal;
import java.security.cert.X509Certificate;
import java.util.HashSet;
import java.util.Set;

/**
 * AuthBroker
 *
 * @author wangrui
 * @date 2022/1/25 11:32
 */
public class AuthBroker extends AbstractAuthenticationBroker {
    public AuthBroker(Broker next) {
        super(next);
    }

    @Override
    public SecurityContext authenticate(String username, String password, X509Certificate[] x509Certificates) throws SecurityException {
        SecurityContext securityContext = new SecurityContext(username) {
            @Override
            public Set<Principal> getPrincipals() {
                HashSet<Principal> principals = new HashSet<>();
//                principals.add(new GroupPrincipal("users"));
                return principals;
            }
        };
        return securityContext;
    }

    @Override
    public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception {

        super.addConnection(context, info);
    }
}
